package com.albaforia.serviceDemo;


import android.app.Service;  
import android.content.Context;
import android.content.Intent;  
import android.media.AudioManager;
import android.os.IBinder;  



public class MyService extends Service{
	boolean isChange;
    int normal;
    int vibrate;
    int silent;
    int currentMode;
    int targetMode;
    long waitingTime;
    AudioManager audio;
    
    @Override  
    public IBinder onBind(Intent intent){  
        return null;
        
    }  
  
    @Override  
    public void onCreate(){  
        super.onCreate();
        isChange = false;
        
    }  
  
    @Override  
    public void onStart(Intent intent, int startId){
        setup(intent);
        if (isChange || targetMode == currentMode)
        	onDestroy();
        
        if(targetMode == normal)
			normal();			
        else if(targetMode == vibrate)
        	vibrate();
        else
        	silent();
	
	}
    
    @Override  
    public void onDestroy(){  
        super.onDestroy();  
    }  
    
    public void setup(Intent intent){
        normal = intent.getIntExtra("normal", 0);
        vibrate = intent.getIntExtra("vibrate", 0);
        silent  = intent.getIntExtra("silent", 0);
        currentMode  = intent.getIntExtra("currentMode", 0);
        targetMode  = intent.getIntExtra("targetMode", 0);
        waitingTime  = intent.getLongExtra("waitingTime", 0);
        audio  = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    }
    
    // 正常
 	protected void normal(){
 		Thread thread = new Thread(){
 	        @Override
 	        public void run(){
 	            try {
 	                synchronized(this){
 	                	while(System.currentTimeMillis() < waitingTime)
 	                		wait(1000);
 	                	
 	                }
 	            }
 	            catch(InterruptedException ex){                    
 	            }
 	           
 	            
 	            audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
 	            isChange = true;	
 	            
 	        }
 	    };
 	    
 	    
 	    
 	    
 	    	thread.start();         	    	
 	    
 	}
 	// 震動
 	protected void vibrate(){
 		Thread thread = new Thread(){
 	        @Override
 	        public void run(){
 	            try {
 	                synchronized(this){
 	                	while(System.currentTimeMillis() < waitingTime)
 	                		wait(1000);
 	                
 	                }
 	            }
 	            catch(InterruptedException ex){                    
 	            }
 	            
 	            
 	            audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
 	   			isChange = true;	
 	            
 	        }
 	    };
 	    
 	    
 	    
 	    
 	    thread.start();         	    	
 	    
 	}
 		
 	// 靜音
 	protected void silent(){
 		Thread thread = new Thread(){
 	        @Override
 	        public void run(){
 	            try {
 	                synchronized(this){
 	                	while(System.currentTimeMillis() < waitingTime)
 	                		wait(1000);
 	                	
 	                }
 	            }
 	            catch(InterruptedException ex){                    
 	            }
 	            
 	            
 	            audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
 	   			isChange = true;		
 	            
 	        }
 	    };
 	    
 	    
 	    
 	    
 	    thread.start();         	    			
 		
 	}
 	
}  